﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using FrameWork;
using FrameWork.Components;
using FrameWork.WebControls;

namespace FrameWork.web.Module.ITMS.Upload.News
{
    public partial class Manager : System.Web.UI.Page
    {
        Int32 IDX = (Int32)Common.sink("IDX", MethodType.Get, 10, 0, DataType.Int);
        public string CMD = (string)Common.sink("CMD", MethodType.Get, 10, 1, DataType.Str);

        protected void Page_Load(object sender, EventArgs e)
        {
            HeadMenuWebControls1.HeadTitleTxt = TitleName_Config;
            HeadMenuWebControls1.ButtonList[0].ButtonName = TitleName_Config;

            if (!Page.IsPostBack)
            {
                FrameWorkPermission.CheckPagePermission(CMD);

                BusinessFacade.CreateDir(Server.MapPath(Common.UpLoadDir + FileUploadPath));
                OnStartConfig();
                OnStart();

            }
        }

        /// <summary>
        /// 初始化配置
        /// </summary>
        private void OnStartConfig()
        {
            //初始化分类
            string[] A_Cats = A_Cats_Config.Split(',');
            foreach (string var in A_Cats)
            {
                ListItem li = new ListItem();
                li.Text = BusinessFacade.sys_FieldValueDisp(Convert.ToInt32(var)).V_Text;
                li.Value = var;
                A_Cat.Items.Add(li);
            }

            if (A_CatsName_Config == "")
            {
                A_CatsNameSearch.Visible = false;
            }
            else
            {
                A_CatsNameSearchTxt.Text = A_CatsName_Config;
            }

            if (A_Title_Config == "")
            {
                A_TitleSearch.Visible = false;
            }
            else
            {
                A_TitleSearchTxt.Text = A_Title_Config;
            }


            if (A_TitleSub_Config == "")
            {
                A_TitleSubSearch.Visible = false;
            }
            else
            {
                A_TitleSubSearchTxt.Text = A_TitleSub_Config;
            }

            if (A_DateTime_Config == "")
            {
                A_DateTimeSearch.Visible = false;
            }
            else
            {
                A_DateTimeSearchTxt.Text = A_DateTime_Config;
            }

            if (A_Photo_Config == "")
            {
                A_PhotoSearch.Visible = false;
            }
            else
            {
                A_PhotoSearchTxt.Text = A_Photo_Config;
            }

            if (A_Txt_Config == "")
            {
                A_TxtSearch.Visible = false;
            }
            else
            {
                A_TxtSearchTxt.Text = A_Txt_Config;
            }
        }

        /// <summary>
        /// 开始操作
        /// </summary>
        private void OnStart()
        {
            FRAMEWORK_ARTICLEEntity ut = BusinessFacade.FRAMEWORK_ARTICLEDisp(IDX);

            bool CheckCatID = false;
            foreach (string var in A_Cats_Config.Split(','))
            {
                if (var.Trim() == ut.A_Cat.ToString())
                {
                    CheckCatID = true;
                    break;
                }
            }

            if (!CheckCatID && ut.A_Cat != 0)
                EventMessage.MessageBox(1, "非法传入值", "IDX:" + ut.A_Cat.ToString() + "不在当前配置中!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
            FrameWorkPermission.CheckPagePermission(CMD);



            switch (CMD)
            {
                case "New":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "增加" + TitleName_Config;
                    Hidden_Disp();
                    break;
                case "List":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "查看" + TitleName_Config;
                    Hidden_Input();
                    ButtonOption.Visible = false;
                    AddEditButton();
                    break;
                case "Edit":
                    TabOptionItem1.Tab_Name = HeadMenuWebControls1.HeadOPTxt = "修改" + TitleName_Config;
                    Hidden_Disp();
                    AddDeleteButton();
                    break;
                case "Delete":
                    ut.DataTable_Action_ = DataTable_Action.Delete;
                    if (BusinessFacade.FRAMEWORK_ARTICLEInsertUpdateDelete(ut) > 0)
                    {
                        FileUpLoadCommon.DeleteFile(string.Format("{0}{1}{2}", Common.UpLoadDir, FileUploadPath + "/", ut.A_Photo));
                        FileUpLoadCommon.DeleteFile(string.Format("{0}{1}s_{2}", Common.UpLoadDir, FileUploadPath + "/", ut.A_Photo));

                        EventMessage.MessageBox(1, "删除成功", string.Format("删除ID:{0}成功!", IDX), Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    else
                    {
                        EventMessage.MessageBox(1, "删除失败", string.Format("删除ID:{0}失败!", IDX), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    }
                    break;
                default:
                    EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
                    break;
            }
            OnStartData(ut);
        }

        /// <summary>
        /// 增加修改按钮
        /// </summary>
        private void AddEditButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Edit;
            bi.ButtonName = TitleName_Config;
            bi.ButtonUrl = string.Format("?CMD=Edit&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);
        }


        /// <summary>
        /// 增加删除按钮
        /// </summary>
        private void AddDeleteButton()
        {
            HeadMenuButtonItem bi = new HeadMenuButtonItem();
            bi.ButtonPopedom = PopedomType.Delete;
            bi.ButtonName = TitleName_Config;
            bi.ButtonUrlType = UrlType.JavaScript;
            bi.ButtonUrl = string.Format("DelData('?CMD=Delete&IDX={0}')", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi);

            HeadMenuButtonItem bi1 = new HeadMenuButtonItem();
            bi1.ButtonPopedom = PopedomType.List;
            bi1.ButtonIcon = "back.gif";
            bi1.ButtonName = "返回";
            bi1.ButtonUrl = string.Format("?CMD=List&IDX={0}", IDX);
            HeadMenuWebControls1.ButtonList.Add(bi1);
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        /// <param name="ut"></param>
        private void OnStartData(FRAMEWORK_ARTICLEEntity ut)
        {
            A_Cat.SelectedValue = ut.A_Cat.ToString();
            A_Cat_Disp.Text = BusinessFacade.sys_FieldValueDisp(ut.A_Cat).V_Text;
            A_Title.Text = A_Title_Disp.Text = ut.A_Title.ToString();
            A_TitleSub.Text = A_TitleSub_Disp.Text = ut.A_TitleSub.ToString();
            A_Photo.Text = ut.A_Photo.ToString();
            if (ut.A_Photo != "")
                A_Photo_Disp.Text = string.Format("<img src='{0}{1}/s_{2}' border='0'>", Common.UpLoadDir, FileUploadPath, ut.A_Photo);
            A_Txt.Text = A_Txt_Disp.Text = ut.A_Txt.ToString();
            A_DateTime_Disp.Text = ut.A_DateTime.ToString();
        }

        /// <summary>
        /// 隐藏输入框
        /// </summary>
        private void Hidden_Input()
        {
            A_Cat.Visible = false;
            A_Title.Visible = false;
            A_TitleSub.Visible = false;
            A_Photo.Visible = false;
            A_Txt.Visible = false;
            A_Photo_FileUpload.Visible = false;
            //A_DateTime.Visible = false;
        }

        /// <summary>
        /// 隐藏显示框
        /// </summary>
        private void Hidden_Disp()
        {
            A_Cat_Disp.Visible = false;
            A_Title_Disp.Visible = false;
            A_TitleSub_Disp.Visible = false;
            //A_Photo_Disp.Visible = false; 
            A_Txt_Disp.Visible = false;
            A_DateTime_Disp.Visible = false;
            A_DateTimeSearch.Visible = false;
        }

        /// <summary>
        /// 增加/修改事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            int A_Cat_Value = (int)Common.sink(A_Cat.UniqueID, MethodType.Post, 10, 0, DataType.Int);
            if (A_Cat_Value == 0)
                A_Cat_Value = Convert.ToInt32(A_Cats_Config.Split(',')[0]);
            string A_Title_Value = (string)Common.sink(A_Title.UniqueID, MethodType.Post, 255, 1, DataType.Str);
            string A_TitleSub_Value = (string)Common.sink(A_TitleSub.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string A_Photo_Value = (string)Common.sink(A_Photo.UniqueID, MethodType.Post, 255, 0, DataType.Str);
            string A_Txt_Value = (string)Common.sink(A_Txt.UniqueID, MethodType.Post, 0, 1, DataType.Str);
            //DateTime A_DateTime_Value = (DateTime)Common.sink(A_DateTime.UniqueID, MethodType.Post, 20, 0, DataType.Dat);



            FRAMEWORK_ARTICLEEntity ut = BusinessFacade.FRAMEWORK_ARTICLEDisp(IDX);
            ut.A_Cat = A_Cat_Value;
            ut.A_Title = A_Title_Value;
            ut.A_TitleSub = A_TitleSub_Value;
            ut.A_Photo = A_Photo_Value;
            ut.A_Txt = A_Txt_Value;
            //ut.A_DateTime = DateTime.Now();

            //处理上传文件Start
            if (A_Photo_FileUpload.Visible == true)
            {
                FileUpLoadCommon fc = new FileUpLoadCommon(Common.UpLoadDir + FileUploadPath + "/", false);
                fc.SaveFile(A_Photo_FileUpload.PostedFile, true);
                if (fc.newFileName != "")
                {
                    FileUpLoadCommon.DeleteFile(string.Format("{0}{1}{2}", Common.UpLoadDir, FileUploadPath + "/", ut.A_Photo));
                    FileUpLoadCommon.DeleteFile(string.Format("{0}{1}s_{2}", Common.UpLoadDir, FileUploadPath + "/", ut.A_Photo));
                    ut.A_Photo = fc.newFileName;
                }
                fc = null;
            }
            //处理上传文件End

            if (CMD == "New")
            {
                ut.DataTable_Action_ = DataTable_Action.Insert;
            }
            else if (CMD == "Edit")
            {
                ut.DataTable_Action_ = DataTable_Action.Update;
            }
            else
            {
                EventMessage.MessageBox(2, "不存在操作字符串!", "不存在操作字符串!", Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
            }
            Int32 rInt = BusinessFacade.FRAMEWORK_ARTICLEInsertUpdateDelete(ut);
            if (rInt > 0)
            {
                string OpTxt = string.Format("增加{1}成功!(ID:{0})", rInt, TitleName_Config);
                if (ut.DataTable_Action_ == DataTable_Action.Update)
                    OpTxt = string.Format("修改{1}成功!(ID:{0})", IDX, TitleName_Config);
                EventMessage.MessageBox(1, "操作成功", OpTxt, Icon_Type.OK, Common.GetHomeBaseUrl("Default.aspx"));
            }
            else if (rInt == -2)
            {
                EventMessage.MessageBox(1, "操作失败", "操作失败,存在相同的键值(用户名/数据)!", Icon_Type.Alert, Common.GetHomeBaseUrl("Default.aspx"));
            }
            else
            {
                EventMessage.MessageBox(1, "操作失败", string.Format("操作失败,返回值:{0}!", rInt), Icon_Type.Error, Common.GetHomeBaseUrl("Default.aspx"));
            }
        }


        #region "读取配置"
        private string TitleName_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["TitleName"].ToString() + "";
            }
        }

        private string A_ID_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_ID"] + "";
            }
        }

        private string A_Cats_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Cats"] + "";
            }
        }

        private string A_CatsName_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_CatsName"] + "";
            }
        }

        private string A_Title_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Title"] + "";
            }
        }
        private string A_TitleSub_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_TitleSub"] + "";
            }
        }
        private string A_Photo_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Photo"] + "";
            }
        }

        private string A_Txt_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_Txt"] + "";
            }
        }
        private string A_DateTime_Config
        {
            get
            {
                return ConfigurationManager.AppSettings["A_DateTime"] + "";
            }
        }

        private string FileUploadPath
        {
            get
            {
                return ConfigurationManager.AppSettings["FileUploadPath"] + "";
            }
        }
        #endregion
    }
}
